package com.xh.bussiness.tiku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.tiku.bean.request.QuestionKnowledgeRequest;
import com.xh.bussiness.tiku.model.QuestionKnowledge;
import com.xh.bussiness.tiku.dao.QuestionKnowledgeMapper;
import com.xh.bussiness.tiku.service.IQuestionKnowledgeService;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.StringUtils;
import org.springframework.stereotype.Service;
import com.xh.core.bean.XhPage;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 试题知识点表 服务实现类
 * </p>
 *
 * @author daiwei
 * @since 2019-01-14
 */
@Service
public class QuestionKnowledgeServiceImpl extends BaseServiceImpl<QuestionKnowledgeMapper, QuestionKnowledge> implements IQuestionKnowledgeService {
    @Override
    public XhPage queryQuestionKnowledgePage(XhPage page, QuestionKnowledgeRequest bean) {
        QueryWrapper qw=new QueryWrapper<QuestionKnowledge>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List<QuestionKnowledge> queryQuestionKnowledge(QuestionKnowledgeRequest bean) {
        QueryWrapper qw=new QueryWrapper<QuestionKnowledge>();
        //拼装条件
        qw=prepareParams(bean, qw);
        if(CollectionUtils.isEmpty(bean.getKnowledgeIds())){
            qw.in("knowledge_id",bean.getKnowledgeIds());
        }

        return this.dao.selectList(qw);
    }


    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(QuestionKnowledgeRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        if (!StringUtils.isEmpty(bean.getQuestionId())) {
            qw.eq("question_id", bean.getQuestionId());
        }
        return qw;
    }

	@Override
	public void deleteByQuestionId(String questionId) {
		this.dao.deleteByQuestionId(questionId);
		
	}
	
	@Override
    public List<Integer> queryQuestionKnowledgeCount(List<String> questionIds) {
        return this.dao.queryQuestionKnowledgeCount(questionIds);
    }
}
